package org.example.hospital_frontend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.example.hospital_frontend.common.ErrorCode;
import org.example.hospital_frontend.domain.User;
import org.example.hospital_frontend.exception.BusinessException;
import org.example.hospital_frontend.service.UserService;
import org.example.hospital_frontend.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Administrator
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-05-22 12:59:37
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    private UserMapper userMapper;
    private static final String SALT = "hospital";
    @Override
    public long userRegister(String username, String password, String checkPassword,Long role,Long gender,String nickname) {
        if(StringUtils.isAnyBlank(username,password,checkPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户名或者密码不能为空！");
        }
        if(username.length()<4){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户名长度不能小于4！");
        }
        if (password.length()<8){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"密码长度不能小于4！");
        }
        if(!password.equals(checkPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"两次密码输入不一致！");
        }
        if (role==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户身份不明");
        }
        //判断特殊字符
        String validPattern = "\\pP|\\pS|\\s+";
        Matcher matcher = Pattern.compile(validPattern).matcher(username);
        if (matcher.find()){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号不能包含特殊字符");
        }
        //密码和校验密码相同
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        long count = this.count(queryWrapper);
        if (count>=1){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"该用户名已存在");
        }
        //密码加密存储
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT+password).getBytes(StandardCharsets.UTF_8));
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(Math.toIntExact(role));
        user.setGender(gender);
        user.setNickname(nickname);
        boolean result = this.save(user);
        if (!result){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"注册失败！");
        }
        return user.getId();
    }

    @Override
    public User doLogin(String userAccount, String password,Long userRole, HttpServletRequest httpServletRequest) {
        if(StringUtils.isAnyBlank(userAccount,password)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户名或者密码不能为空！");
        }
        if (userAccount.length()<4){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户名或者密码错误！");
        }
        if (password.length()<8){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户名或者密码错误！");
        }
        //判断特殊字符
        String validPattern = "\\pP|\\pS|\\s+";
        Matcher matcher = Pattern.compile(validPattern).matcher(userAccount);
        if (matcher.find()){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号不能包含特殊字符");
        }
        //校验密码是否正确
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        String encryptPassword = DigestUtils.md5DigestAsHex((SALT+password).getBytes(StandardCharsets.UTF_8));
        queryWrapper.eq("username",userAccount);
        queryWrapper.eq("password",password);
        queryWrapper.eq("role",userRole);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号或密码错误,请重新尝试!");
        }
        //记录用户的登录态
        httpServletRequest.getSession().setAttribute("USER_LOGIN_STATE",user);
        return user;
    }
}




